import urllib,urllib2,re,xbmcplugin,xbmcgui,xbmc
import sys, os, os.path

#author:
#elypter
#-mail:elypter@yahoo.de

#description:
#This plugin is a simple search-plugin for www.divxstage.net
#
#-If you think something in this plugin needs to be changed, send an email

#version-history:
#1_b1:
#-first release
#-I cant imagine any missing feature, so i guess it will be also the final release

baseurl="http://www.divxstage.net"
moviemax=100

def get_regexp():
	return 'http\://divxstage\.net/player\.php\?v\=([^<>"]*)'
def get_name():
	return "divxstage"

def join(id):
	return "http://www.divxstage.net"+"/video/"+id

def get_link1(url):
	req = urllib2.Request(url)
	req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
	response = urllib2.urlopen(req)
	page=response.read()
	response.close()
	vurl=re.compile('\<param name="src" value="([^<>]*)" /\>').findall(page)
	return ('divxstage',vurl[0],'')
	
def get_link2(param,code):
	#should take the url that get_link1 returned
	#
	#returnes the video url
	return ()
	
def addLink(name,url,iconimage):
        ok=True
        liz=xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage)
        liz.setInfo( type="Music", infoLabels={ "Title": name } )
        ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=url,listitem=liz)
        return ok
		
def VIDEOLINKS(url,moviec):
		#possibly preventing infinite loops
		if not moviec<moviemax:
			return
		
		#get search results
		req = urllib2.Request(url)
		req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
		response = urllib2.urlopen(req)
		link=response.read()
		response.close()
		match=re.compile("\<a href=['\"]"+baseurl+"/video/([^<>]*)['\"] target=['\"]_blank['\"]\>\<img src=['\"]thumbs/([^<>]*)['\"] border=['\"][\d]*['\"]\>\<br\>([^<>]*)\<\/a\>").findall(link)
		
		#get links, thumbs, titles and list them
		for mid,thumbid,title in match:
			req = urllib2.Request(baseurl+"/video/"+mid)
			req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
			response = urllib2.urlopen(req)
			page=response.read()
			response.close()
			vurl=re.compile('\<param name="src" value="([^<>]*)" /\>').findall(page)
			if len(vurl)>0:
				addLink(title,vurl[0],baseurl+'/thumbs/'+str(thumbid))
		
		#look for next page
		next=re.compile("\<a href=['\"]([^<>'\"]*)['\"]\>Next[^<>]*\</a\>").findall(link)
		if len(next)>0 :
			#append following pages
			VIDEOLINKS(baseurl+'/'+next[0],moviec+16)

"""			
kb = xbmc.Keyboard('default', 'heading', False)
kb.setDefault('') # optional
kb.setHeading('Enter search') # optional
kb.doModal()
if (kb.isConfirmed()):
	search = kb.getText()
	VIDEOLINKS(baseurl+'/search.php?s='+search+'&p='+str(0*16),0)	
	

xbmcplugin.endOfDirectory(int(sys.argv[1]))
"""